package com.ruoyi.web.controller.system;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.ACustomer;
import com.ruoyi.system.domain.AGift;
import com.ruoyi.system.domain.GiftChangeReport;
import com.ruoyi.system.domain.dto.GiftChangeDto;
import com.ruoyi.system.domain.vo.ChangeRtnMessageVo;
import com.ruoyi.system.service.IACustomerService;
import com.ruoyi.system.service.IAGiftService;
import com.ruoyi.system.service.IGiftChangeReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

/**
 * @Package: com.ruoyi.web.controller.system
 * @ClassName: GiftChangeController.java
 * @Description: 礼物兑换
 * @author: zhengxiaolong
 * @date: 2024/11/1 15:22
 */
@Controller
@RequestMapping("/system/gift/change")
public class GiftChangeController {
    private String prefix = "system/gift/change";

    @Autowired
    private IAGiftService aGiftService;
    @Autowired
    private IACustomerService customerService;

    @Autowired
    private IGiftChangeReportService giftChangeReportService;

    @RequiresPermissions("system:gift:change:view")
    @GetMapping()
    public String gift() {
        return prefix + "/change";
    }

    @PostMapping("/doChange")
    @ResponseBody
    public ChangeRtnMessageVo giftChange(@RequestBody GiftChangeDto giftChangeDto){
        ChangeRtnMessageVo changeRtnMessageVo = new ChangeRtnMessageVo();
        if (Objects.isNull(giftChangeDto.getGiftId()) || Objects.isNull(giftChangeDto.getMobile())){
            changeRtnMessageVo.setFlag(false);
            return changeRtnMessageVo;
        }
//        查手机号存不存在
        ACustomer customer = customerService.getByMobile(giftChangeDto.getMobile());
        if (Objects.isNull(customer)){
            changeRtnMessageVo.setFlag(false);
            changeRtnMessageVo.setMessage("该手机号不是本店会员，要兑换礼物要先成为本店会员哦~");
            return changeRtnMessageVo;
        }

//        查兑换的礼物
        AGift gift = aGiftService.selectAGiftById(giftChangeDto.getGiftId());
        if (customer.getBalance() <= gift.getGiftScore()){
            changeRtnMessageVo.setFlag(false);
            changeRtnMessageVo.setMessage("您的积分余额不足~");
            return changeRtnMessageVo;
        }

        if (Objects.isNull(gift) || gift.getGiftStock() == 0){
            changeRtnMessageVo.setFlag(false);
            changeRtnMessageVo.setMessage("您所想兑换的商品不存在，或库存不足~");
            return changeRtnMessageVo;
        }

        if (customer.getBalance() < gift.getGiftScore()){
            changeRtnMessageVo.setFlag(false);
            changeRtnMessageVo.setMessage("你所剩的积分不足啦~不可以兑换礼物哦~");
            return changeRtnMessageVo;
        }

        Long giftScore = gift.getGiftScore();
        customer.setBalance(customer.getBalance() - giftScore);
        customerService.updateACustomer(customer);
        gift.setGiftStock(gift.getGiftStock() - 1);
        aGiftService.updateAGift(gift);

        changeRtnMessageVo.setGiftName(gift.getGiftName());
        changeRtnMessageVo.setBalance(customer.getBalance());
        changeRtnMessageVo.setCustomerName(customer.getCustomerName());
        changeRtnMessageVo.setMessage();


//        保存礼物兑换记录
        GiftChangeReport giftChangeReport = new GiftChangeReport();
        giftChangeReport.setGiftName(gift.getGiftName());
        giftChangeReport.setGiftScore(gift.getGiftScore());
        giftChangeReport.setCreateUser(ShiroUtils.getUserId());
        giftChangeReport.setMobile(giftChangeDto.getMobile());
        giftChangeReport.setSocreBalance(customer.getBalance() - giftScore);
        giftChangeReport.setCreateTime(new Date());
        giftChangeReportService.insertGiftChangeReport(giftChangeReport);

        return changeRtnMessageVo;
    }
}